﻿using System;
using System.IO;
using System.Xml;
using PSE.Deployment.Engine.CustomEventArgs;
using PSE.Deployment.SetupBase;
using PSE.Deployment.SetupBase.Configuration.Enums;
using PSE.Deployment.SetupBase.Parameter;
using PSE.Deployment.SetupBase.Parameter.Enums;
using Sinacor.Deployment.Setup.Configuration;
using Sinacor.Deployment.Setup.Install;
using Sinacor.Deployment.Setup.Parameter;
using Sinacor.Deployment.Setup.Wizard;

namespace Sinacor.Deployment.Setup.Main
{
    public class SinacorMain : SetupMain
    {
        #region Fileds

        private WizardGroup _wizardGroup;

        #endregion

        #region Properties

        public new SinacorParameter Parameter
        {
            get
            {
                return (SinacorParameter)base.Parameter;
            }
        }

        public new SinacorConfiguration Configuration
        {
            get
            {
                return (SinacorConfiguration)base.Parameter.SetupConfiguration;
            }
        }

        #endregion

        #region Constructor

        public SinacorMain()
            : base()
        {
            this.LogPath = Path.Combine(Environment.GetEnvironmentVariable("TEMP", EnvironmentVariableTarget.Machine), System.Configuration.ConfigurationSettings.AppSettings["LogFilePath"]);
        }

        #endregion

        #region Group Events

        protected override void SetupMain_ExecutionStarted(object sender, ExecutionStartedEventArgs e)
        {
            base.SetupMain_ExecutionStarted(sender, e);

            if (e.OriginalSource == this)
            {
                SetupMainFactory.GetInstance().Parameter.Prerequisites.IIS7 = SetupMainFactory.GetInstance<SinacorMain>().Parameter.Prerequisites.IIS7;
            }
        }

        protected override void SetupMain_ExecutionFinished(object sender, ExecutionFinishedEventArgs e)
        {
            if (e.OriginalSource == this && SetupMainFactory.GetInstance<SinacorMain>().Parameter.IsInstalled && SetupMainFactory.GetInstance<SinacorMain>().Parameter.SetupMode == SetupMode.Install)
            {
                StartConfigurationManager();
            }

            base.SetupMain_ExecutionFinished(sender, e);
        }

        private void _wizardGroup_ExecutionFinished(object sender, ExecutionFinishedEventArgs e)
        {
            if (e.OriginalSource is WizardGroup)
            {
                if (!SetupMainFactory.GetInstance<SinacorMain>().Parameter.IsInstalled)
                {
                    SetupMainFactory.GetInstance<SinacorMain>().Parameter.SetupMode = SetupMode.Install;
                    AddInstallStep();
                }
                else
                {
                    AddChangeStep();
                }
            }
        }

        #endregion

        #region Virtual Methods

        protected override Type GetConfigurationType()
        {
            return typeof(SinacorConfiguration);
        }

        protected override Type GetParameterType()
        {
            return typeof(SinacorParameter);
        }

        protected override void CreateParameter()
        {
            base.Parameter = new SinacorParameter();
        }

        protected override void AddInstallStep()
        {
            this.Steps.Add(new SinacorInstall(this));
        }

        protected override void AddChangeStep()
        {
            this.Steps.Add(new SinacorChange(this));
        }

        protected override void AddRepairStep()
        {
            this.Steps.Add(new SinacorRepair(this));
        }

        protected override void AddUpdateStep()
        {
            this.Steps.Add(new SinacorUpdate(this));
        }

        protected override void AddWizardStep()
        {
            _wizardGroup = new WizardGroup(this);
            _wizardGroup.ExecutionFinished += new EventHandler<ExecutionFinishedEventArgs>(_wizardGroup_ExecutionFinished);
            this.Steps.Add(_wizardGroup);
        }

        #endregion

        #region Configuration Manager

        private void StartConfigurationManager()
        {
            String fileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar + "SinacorConfigurationManager.exe";

            if (File.Exists(fileName))
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.FileName = fileName;
                process.Start();
            }
        }

        #endregion
    }
}